package cn.meibang.common.beauty.provider.service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.apestar.core.conf.CommonConfig;
import cn.apestar.core.http.HeaderOSType;
import cn.apestar.core.json.JsonConverter;
import cn.apestar.core.utils.CustomHttpResponse;
import cn.apestar.core.utils.HttpUtils;
import cn.meibang.common.beauty.api.IMeibangUserAPIService;
import cn.meibang.common.beauty.provider.vo.AddScoreVo;
import cn.meibang.common.beauty.provider.vo.MeibangUserInfoListVo;
import cn.meibang.common.beauty.provider.vo.MeibangUserInfoVo;

/**
 * Created by admin on 2017-6-17.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = "dubbo", timeout = 5000)
public class MeibangUserAPIServiceProvider implements IMeibangUserAPIService {

    @Autowired
    private JsonConverter jsonConverter;

    @Autowired
    private CommonConfig commonConfig;

    /**
     * 模拟访问：http://nstaff.meibangtech.com/?task=_getAppCustomerList&member_ids=100218,100286
     * @param userIds
     * @param ticket
     * @return
     */
    @Override
    public List<MeibangUserInfoVo> listMeibangUserInfo(List<Long> userIds, String ticket) {
        if (userIds == null || userIds.size() == 0) {
            return Collections.emptyList();
        }
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", ProductAPIServiceProvider.HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());
        headerMap.put("ticket", ticket);
        // headerMap.put("ticket", "TGT-f026e86a4ed2584a200d98a20acb2418.wtp.com");

        Map<String, String> paramMap = new HashMap();
        paramMap.put("task", "_getAppCustomerList");
        paramMap.put("member_ids", StringUtils.join(userIds, ","));

        CustomHttpResponse customHttpResponse = HttpUtils.httpGetCustomerHttpResponse(paramMap, headerMap, commonConfig.getThirdUrl() + "/php", "");
        if (customHttpResponse.getStatus() == HttpStatus.SC_OK) {
            MeibangUserInfoListVo meibangUserInfoListVo = jsonConverter.toObject(customHttpResponse.getBody(), MeibangUserInfoListVo.class);
            return meibangUserInfoListVo.getList();
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public MeibangUserInfoVo getMeibangUserInfoByOpenid(String openid, String ticket) {
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", ProductAPIServiceProvider.HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());
        headerMap.put("ticket", ticket);
        // headerMap.put("ticket", "TGT-3347ec45e32f797725b1ad35ea3b73a1.apestar.com");

        Map<String, String> paramMap = new HashMap();
        paramMap.put("task", "_getWxCustomerInfo");
        paramMap.put("openid", openid);
        // paramMap.put("openid", "oC65QwFGc78K3UBVLkflFIOq2SOA");

        CustomHttpResponse customHttpResponse = HttpUtils.httpGetCustomerHttpResponse(paramMap, headerMap, commonConfig.getThirdUrl() + "/php", "");
        if (customHttpResponse.getStatus() == HttpStatus.SC_OK) {
            MeibangUserInfoVo meibangUserInfoVo = jsonConverter.toObject(customHttpResponse.getBody(), MeibangUserInfoVo.class);
            return meibangUserInfoVo;
        } else {
            return null;
        }
    }

    /**
     * @param score 分数
     * @param scoreType 1:分享听美丽，2：分享看美丽， 3：作品
     * @param ticket
     * @return
     */
    @Override
    public AddScoreVo addScore(int score, int scoreType, String ticket) {
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", ProductAPIServiceProvider.HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());
        headerMap.put("ticket", ticket);
        // headerMap.put("ticket", "TGT-3347ec45e32f797725b1ad35ea3b73a1.apestar.com");

        Map<String, String> paramMap = new HashMap();
        paramMap.put("task", "_addStoreUserScore");
        paramMap.put("score", String.valueOf(score));
        paramMap.put("score_type", String.valueOf(scoreType));
        // paramMap.put("openid", "oC65QwFGc78K3UBVLkflFIOq2SOA");

        CustomHttpResponse customHttpResponse = HttpUtils.httpGetCustomerHttpResponse(paramMap, headerMap, commonConfig.getThirdUrl() + "/php", "");
        if (customHttpResponse.getStatus() == HttpStatus.SC_OK) {
            AddScoreVo addScoreVo = jsonConverter.toObject(customHttpResponse.getBody(), AddScoreVo.class);
            return addScoreVo;
        } else {
            return null;
        }
    }
}
